package com.yunmaozj.tools.site.server.controller;

import com.yunmaozj.tools.site.server.config.ResourceCacheManager;
import com.yunmaozj.tools.site.server.dto.Developer;
import com.yunmaozj.tools.site.server.dto.DocsList;
import com.yunmaozj.tools.site.server.util.DocumentMetadata;
import com.yunmaozj.tools.site.server.util.FileManager;
import com.yunmaozj.tools.site.server.util.SiteMetadata;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Session;
import org.dom4j.DocumentException;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import static java.util.Objects.isNull;

/**
 * @author zhongminghong
 * @email zhongmh@yunmaozj.com
 * @create 2022-05-26 23:31
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/site")
public class SiteController {

    private FileManager fileManager;
    private ResourceCacheManager resourceCacheManager;

    @PostMapping("/uploadFile/{projectId}")
    public HttpEntity<String> uploadFile(@PathVariable("projectId") String projectId, String path, MultipartFile file) {
        try {
            if (fileManager.existsMetadataFile(projectId)) {
                fileManager.uploadFile(projectId, path, file);
            } else {
                return ResponseEntity.status(403).body("当前项目不存在项目元数据");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok().build();
    }

    @PostMapping("/uploadMetadata/{projectId}")
    public HttpEntity<Integer> uploadDocumentMeta(@PathVariable("projectId") String projectId, @RequestBody List<Developer> developers) throws DocumentException {
        DocumentMetadata documentMeta = DocumentMetadata.load(fileManager, projectId);
        documentMeta.addDeveloper(developers);
        SiteMetadata siteMetadata = SiteMetadata.load(fileManager);
        siteMetadata.addProject(developers, projectId);
        return ResponseEntity.ok().build();
    }

    @PostMapping("login")
    public HttpEntity<String> login(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response) throws DocumentException {
        SiteMetadata siteMetadata = SiteMetadata.load(fileManager);
        if (!siteMetadata.existsUser(username, password)) {
            return ResponseEntity.notFound().build();
        }
        Cookie cookie = WebUtils.getCookie(request, "JSESSIONID");
        String sessionId = isNull(cookie) ? null : cookie.getValue();
        if (isNull(sessionId)) {
            HttpSession session = request.getSession(true);
            sessionId = session.getId();
        }
        resourceCacheManager.addLoginInfo("_"+sessionId, username);
        return ResponseEntity.ok().build();
    }
}
